package com.gxd.base;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxd.base.utils.JedisTool;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class SessionInterceptor implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("application/json; charset=utf-8");
    	response.setHeader("Access-Control-Allow-Headers", "*");
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setHeader("Access-Control-Allow-Credentials","true");
    	response.setHeader("Access-Control-Max-Age", "3600");
        System.out.println("================postHandle======================");

        //----------重设salt过期时间--------------
        JedisTool jedis = new JedisTool();
        Cookie userCookie = jedis.resetCookie(request);
        response.addCookie(userCookie);
        //----------------end-------------------

    	/*Cookie[] cookies = request.getCookies();
    	if(cookies != null){
    		for(Cookie ck : cookies){
        		if(ck.getName().equals("token")){
        	    	String token = MD5.passwordEncrypt(request.getParameter("userCode"));
        			String ckToken = ck.getValue();
        			//return ckToken.equals(token);
        			System.out.println("验证cookie是否通过："+ckToken.equals(token));
        			return true;
        		}
        	}
    	}*/
    	return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

}